#!/usr/bin/env python

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'discovey_form.ui'
#
# Created: Sun Apr 26 12:48:05 2009
#      by: The PyQt User Interface Compiler (pyuic) 3.17.4
#
# WARNING! All changes made in this file will be lost!


from qt import *
from main import *
import os


class Discovey_form(QDialog):
    def __init__(self,parent = None,name = None,modal = 0,fl = 0):
        QDialog.__init__(self,parent,name,modal,fl)

        if not name:
            self.setName("Discovey_form")



        self.pentest_box = QGroupBox(self,"pentest_box")
        self.pentest_box.setGeometry(QRect(10,70,651,90))

        self.textLabel1 = QLabel(self.pentest_box,"textLabel1")
        self.textLabel1.setGeometry(QRect(10,40,140,20))

        self.ip_network_text = QLineEdit(self.pentest_box,"ip_network_text")
        self.ip_network_text.setGeometry(QRect(160,40,340,21))

        self.discover_ip_button = QPushButton(self.pentest_box,"discover_ip_button")
        self.discover_ip_button.setGeometry(QRect(520,40,110,24))

        self.report_box = QGroupBox(self,"report_box")
        self.report_box.setGeometry(QRect(10,170,651,351))

        self.report_pad = QTextEdit(self.report_box,"report_pad")
        self.report_pad.setReadOnly(1)
        self.report_pad.setPaletteForegroundColor(QColor(0,0,0))
        self.report_pad.setPaletteBackgroundColor(QColor(255,255,255))
        self.report_pad.setGeometry(QRect(10,20,630,320))
        self.report_pad.setWordWrap(QTextEdit.WidgetWidth)

        self.textLabel7 = QLabel(self,"textLabel7")
        self.textLabel7.setGeometry(QRect(90,20,490,25))

        self.Interface_Info_Button = QPushButton(self,"Interface_Info_Button")
        self.Interface_Info_Button.setGeometry(QRect(540,540,118,24))

        self.about_button = QPushButton(self,"about_button")
        self.about_button.setGeometry(QRect(400,540,118,24))

        self.Cancel_pentest_button = QPushButton(self,"Cancel_pentest_button")
        self.Cancel_pentest_button.setGeometry(QRect(260,540,118,24))

        self.languageChange()

        self.resize(QSize(675,579).expandedTo(self.minimumSizeHint()))
        self.clearWState(Qt.WState_Polished)

        self.connect(self.discover_ip_button,SIGNAL("clicked()"),self.discover_ip_button_clicked)
        self.connect(self.Interface_Info_Button,SIGNAL("clicked()"),self.Interface_Info_Button_clicked)
        self.connect(self.about_button,SIGNAL("clicked()"),self.about_button_clicked)
        self.connect(self.Cancel_pentest_button,SIGNAL("clicked()"),self.Cancel_pentest_button_clicked)


    def languageChange(self):
        self.setCaption(self.__tr("Discovery"))
        self.pentest_box.setTitle(self.__tr("Discovering IP/Network"))
        self.textLabel1.setText(self.__tr("IP/Netwotk address"))
        self.discover_ip_button.setText(self.__tr("&Discover"))
        self.discover_ip_button.setAccel(QKeySequence(self.__tr("Alt+D")))
        self.report_box.setTitle(self.__tr("Discovery report"))
        self.report_pad.setText(QString.null)
        self.textLabel7.setText(self.__tr("<p align=\"center\"><b><h3>Penetration Testing Framework(Discovery)<h2></h2></h2></b></p>"))
        self.Interface_Info_Button.setText(self.__tr("&Interface Info"))
        self.Interface_Info_Button.setAccel(QKeySequence(self.__tr("Alt+I")))
        self.about_button.setText(self.__tr("&About"))
        self.about_button.setAccel(QKeySequence(self.__tr("Alt+A")))
        self.Cancel_pentest_button.setText(self.__tr("&Cancel"))
        self.Cancel_pentest_button.setAccel(QKeySequence(self.__tr("Alt+C")))


    def discover_ip_button_clicked(self):
        ip = str(self.ip_network_text.text())

        #check if the user didn't enter an IP Address
        if len(ip) == 0 :
            Message = 'Please enter an IP Address to scan!'
            QMessageBox.critical(self,"Warning!",Message,QMessageBox.Ok)

            return

        #if the user entered an IP address
        else :

            #clear the main Box
            self.report_pad.clear()

            #print wait message
            self.report_pad.append("<b><br>Thanks for using Discovery, Please sit back and we will do the whole job</b>")

            #calling the scan function
            open_ports, output_file = scan(ip, self.report_pad)

            #check if the host Doesn't have any open ports
            if open_ports is None and output_file is None :
                return

            #if the host has some open ports, EXPLOIT
            else :

                #open the configuration file
                conf_file = open('discovery.conf')

                #declare the options dictionary
                options_dictionary = {}

                #initialize the options dictionary
                for raw_option in conf_file :

                    option = raw_option.strip().split()

                    #take only two arguments lines
                    if len(option) == 2 :
                        options_dictionary[option[0]] = option[1]

                #close the configuration file
                conf_file.close()

                #if the metasploit path wasn't defined in the configuration file
                if 'METASPLOIT_PATH' not in options_dictionary.keys() :
                    Message = 'Syntax Error in the discovery configuration file!'
                    QMessageBox.critical(self,"Warning!",Message,QMessageBox.Ok)
                    return

                else :

                    #print the exploitation wait message
                    self.report_pad.append("<b><br><br>Starting the automated penetration testing, This is going to take long time.</b>")
                    self.report_pad.append("<b>So, again sit back and let us do the job.<br></b>")

                    #call the autopwn function to exploit all services
                    if len(open_ports) > 0 :
                        autopwn(ip, output_file, options_dictionary['METASPLOIT_PATH'], open_ports, self.report_pad)

    def Interface_Info_Button_clicked(self):
        cmd = 'ifconfig -a'
        file_result_keep = os.popen(cmd)
        result = file_result_keep.read()
        self.report_pad.clear()
        self.report_pad.append(str(result))


    def about_button_clicked(self):
        Message = 'copyright reserved to fr34cks h4ch3rz'
        QMessageBox.information(self,"Info",Message,QMessageBox.Ok)

    def Cancel_pentest_button_clicked(self):
        Message = 'Are you sure you want to exit!'
        reply = QMessageBox.information(self,"Question!",Message,QMessageBox.Yes,QMessageBox.No)
        if reply == QMessageBox.Yes:
            self.hide()
        else:
            pass

    def __tr(self,s,c = None):
        return qApp.translate("Discovey_form",s,c)
